package insight.sm.controllers;

import java.util.ArrayList;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

public class CommonFunc {


	public static String analyzeRequest(HttpServletRequest request) {
	     //String a = "filter\\[filters\\]\\[[0-9]\\]";
	     String simpleFilterField = "filter\\[filters\\]\\[[0-9]\\]\\[field\\]";
	     String simpleFilterOperator = "filter\\[filters\\]\\[[0-9]\\]\\[operator\\]";
	     String simpleFilterValue = "filter\\[filters\\]\\[[0-9]\\]\\[value\\]";
	     String complexFilterOperator = "filter\\[filters\\]\\[[0-9]\\]\\[logic\\]";
	     String complexFilterField1 = "filter\\[filters\\]\\[[0-9]\\]\\[filters\\]\\[0\\]\\[field\\]";
	     String complexFilterOperator1 = "filter\\[filters\\]\\[[0-9]\\]\\[filters\\]\\[0\\]\\[operator\\]";
	     String complexFilterValue1 = "filter\\[filters\\]\\[[0-9]\\]\\[filters\\]\\[0\\]\\[value\\]";
	     String complexFilterField2 = "filter\\[filters\\]\\[[0-9]\\]\\[filters\\]\\[1\\]\\[field\\]";
	     String complexFilterOperator2 = "filter\\[filters\\]\\[[0-9]\\]\\[filters\\]\\[1\\]\\[operator\\]";
	     String complexFilterValue2 = "filter\\[filters\\]\\[[0-9]\\]\\[filters\\]\\[1\\]\\[value\\]";
	     String filterOperator = "filter\\[logic\\]";
	     Enumeration<String> paramEnum=request.getParameterNames();
	     String op="";
	     ArrayList<String>  _simpleFilterField,_simpleFilterOperator,_simpleFilterValue,
	     					_complexFilterValue2,_complexFilterOperator2,_complexFilterField2,
	     					_complexFilterOperator1,_complexFilterValue1, _filterOperator,
	     					_complexFilterField1,_complexFilterOperator;
	    _simpleFilterField=new ArrayList<String>();
	    _simpleFilterOperator=new ArrayList<String>();
	    _simpleFilterValue=new ArrayList<String>();
	    _complexFilterField1=new ArrayList<String>();
	    _complexFilterField2=new ArrayList<String>();
	    _complexFilterOperator=new ArrayList<String>();
	    _complexFilterOperator1=new ArrayList<String>();
	    _complexFilterOperator2=new ArrayList<String>();
	    _complexFilterValue1=new ArrayList<String>();
	    _complexFilterValue2=new ArrayList<String>();
	    _filterOperator=new ArrayList<String>();
	    while(paramEnum.hasMoreElements()){
	    	 String item=(String) paramEnum.nextElement();
	    	 if(item.matches(simpleFilterField)){
	    		 _simpleFilterField.add(request.getParameter(item));
	    	 }
	    	 else if(item.matches(simpleFilterOperator)){
	     		 _simpleFilterOperator.add(request.getParameter(item));
	    	 }
	    	 else if(item.matches(simpleFilterValue)){
	    		  _simpleFilterValue.add(request.getParameter(item));
	    	 }	
	    	 else if(item.matches(filterOperator)){
	    		 _filterOperator.add(request.getParameter(item));
	    	 }
	    	 else if(item.matches(complexFilterField1)){
	    		 _complexFilterField1.add(request.getParameter(item));
	    	 }
	    	 else if(item.matches(complexFilterOperator)){
	    		 _complexFilterOperator.add(request.getParameter(item));
	    	 }
	    	 else if(item.matches(complexFilterValue1)){
	    		 _complexFilterValue1.add(request.getParameter(item));
	    	 }
	    	 else if(item.matches(complexFilterOperator1)){
	    		 _complexFilterOperator1.add(request.getParameter(item));
	    	 }
	    	 else if(item.matches(complexFilterField2)){
	    		 _complexFilterField2.add(request.getParameter(item));
	    	 }
	    	 else if(item.matches(complexFilterOperator2)){
	    		 _complexFilterOperator2.add(request.getParameter(item));
	    	 }
	    	 else if(item.matches(complexFilterValue2)){
	    		 _complexFilterValue2.add(request.getParameter(item));
	    	 }
	    }
	    if(_complexFilterField1.size()==0 && _complexFilterField2.size()==0 && _simpleFilterField.size()>0){
	    	boolean  isFirst=true;
	    	String pref="";
	    	for(int i=0;i<_simpleFilterField.size();i++){
	    		if(isFirst)
	    			pref="";
	    		else
	    			pref=" "+_filterOperator.get(0)+" ";
	    		op=op+pref+" ("+_simpleFilterField.get(i) +" "+formExpression(_simpleFilterOperator.get(i), _simpleFilterValue.get(i))+") ";
	    		
	    		isFirst=false;
		    }
	    }else if(_simpleFilterField.size()!=0&&_complexFilterField1.size()==_complexFilterField2.size()){
	    	boolean  isFirst=true;
	    	String pref="";
	    	for(int i=0;i<_simpleFilterField.size();i++){
	    		if(isFirst)
	    			pref="";
	    		else
	    			pref=" "+_filterOperator.get(0)+" ";
	    		op=op+pref+" ("+_simpleFilterField.get(i) +" "+formExpression(_simpleFilterOperator.get(i), _simpleFilterValue.get(i))+") ";
	    		
	    		isFirst=false;
		    }
	    	
	    	op=op+_filterOperator.get(0);
	    	
	    	isFirst=true;
	    	for(int i=0;i<_complexFilterField1.size();i++){
	    		if(isFirst)
	    			pref="";
	    		else
	    			pref=" "+_filterOperator.get(0)+" ";
	    		op=op+pref+" ("+_complexFilterField1.get(i) +" "+formExpression(_complexFilterOperator1.get(i), _complexFilterValue1.get(i))+" "+_complexFilterOperator.get(i)+" "+_complexFilterField2.get(i)+" "+formExpression(_complexFilterOperator2.get(i), _complexFilterValue2.get(i))+") ";
	    		isFirst=false;
		    }
	    }
	    else if(_simpleFilterField.size()==0&&_complexFilterField1.size()!=0&&_complexFilterField1.size()==_complexFilterField2.size()){
	    	boolean  isFirst=true;
	    	String pref="";
	    	for(int i=0;i<_complexFilterField1.size();i++){
	    		if(isFirst)
	    			pref="";
	    		else
	    			pref=" "+_filterOperator.get(0)+" ";
	    		op=op+pref+" ("+_complexFilterField1.get(i) +" "+formExpression(_complexFilterOperator1.get(i), _complexFilterValue1.get(i))+" "+_complexFilterOperator.get(i)+" "+_complexFilterField2.get(i)+" "+formExpression(_complexFilterOperator2.get(i), _complexFilterValue2.get(i))+") ";
	    		isFirst=false;
		    }
	    }
	     return op;
	}
	
	private static String formExpression(String item, String value) {
		String op="";
		switch(item){
		 case "eq":
			 op=" = '"+value+"' ";
			 break;
		 case "neq":
			 op=" <> '"+value+"' ";
			 break;
		 case "startswith":
			 op=" like '"+value+"%' ";
			 break;	 
		 case "contains":
			 op=" like '%"+value+"%' ";
			 break;	 
		 case "doesnotcontain":
			 op=" NOT like '%"+value+"%' ";
			 break;	 
		 case "endswith":
			 op=" like '"+value+"%' ";
			 break;	  
		 default:
			 op=item;
			 break;
		 }
		return op;
	}
}